package com.xtoad.ecms.common.utils.generate;

import java.text.MessageFormat;
import java.util.Map;

/**
 * 转换器生成类
 *
 * @author xtoad
 * @date 2021/1/19
 */
public class ConverterGenerator implements IGenerator {
    /**
     * 根据包路径和类名生成类
     *
     * @param packagePath 包路径
     * @param modelName   类名
     * @return 生成的类
     */
    @Override
    public String generate(String packagePath, String modelName) {
        System.out.println("generate Converter start ");
        Map<String, String> classComment = Generator.getClassComment(Generator.modelPackage + "." + modelName);
        StringBuilder sb = new StringBuilder();
        sb.append("/*\r\n")
                .append(" * Copyright  2019-2029  xtoad\r\n")
                .append(" */\r\n")
                .append("package ").append(packagePath).append(";\r\n")
                .append("\r\n")
                .append("import com.xtoad.ecms.baseinfo.dto.{0}DTO;\r\n")
                .append("import com.xtoad.ecms.baseinfo.enums.Sex;\r\n")
                .append("import com.xtoad.ecms.baseinfo.model.{0};\r\n")
                .append("import com.xtoad.ecms.common.web.base.IConverter;\r\n")
                .append("import org.springframework.stereotype.Component;\r\n")
                .append("\r\n")
                .append("import java.util.ArrayList;\r\n")
                .append("import java.util.List;\r\n")
                .append("\r\n")
                .append("/**\r\n")
                .append(" * {1}转换器实现类\r\n")
                .append(" *\r\n")
                .append(" * @author xtoad\r\n")
                .append(" * @date 2020/05/29\r\n")
                .append(" */\r\n")
                .append("@Component\r\n")
                .append("public class {0}Converter implements IConverter<{0}DTO, {0}> '{'\r\n")
                .append("\r\n")
                .append("    /**\r\n")
                .append("     * Model To DTO\r\n")
                .append("     *\r\n")
                .append("     * @param model 源\r\n")
                .append("     * @return 目标\r\n")
                .append("     */\r\n")
                .append("    @Override\r\n")
                .append("    public {0}DTO toDto({0} model) '{'\r\n")
                .append("        {0}DTO dto = new {0}DTO();\r\n");
        for (String filed : classComment.keySet()) {
            if (filed.equals(modelName)) {
                continue;
            }
            String s = Generator.firstCharUpper(filed);
            sb.append("        dto.set").append(s).append("(model.get").append(s).append("());\r\n");
        }
        sb.append("        return dto;\r\n")
                .append("    '}'\r\n")
                .append("\r\n")
                .append("    /**\r\n")
                .append("     * DTO To Model\r\n")
                .append("     *\r\n")
                .append("     * @param dto 源\r\n")
                .append("     * @return 目标\r\n")
                .append("     */\r\n")
                .append("    @Override\r\n")
                .append("    public {0} toModel({0}DTO dto) '{'\r\n")
                .append("        {0} model = new {0}();\r\n");
        for (String filed : classComment.keySet()) {
            if (filed.equals(modelName)) {
                continue;
            }
            String s = Generator.firstCharUpper(filed);
            sb.append("        model.set").append(s).append("(dto.get").append(s).append("());\r\n");
        }
        sb.append("        return model;\r\n")
                .append("    '}'\r\n")
                .append("\r\n")
                .append("    /**\r\n")
                .append("     * 批量Model To DTO\r\n")
                .append("     *\r\n")
                .append("     * @param models 源\r\n")
                .append("     * @return 目标\r\n")
                .append("     */\r\n")
                .append("    @Override\r\n")
                .append("    public List<{0}DTO> toDtoList(List<{0}> models) '{'\r\n")
                .append("        List<{0}DTO> dtoList = new ArrayList<>();\r\n")
                .append("        models.forEach(model -> dtoList.add(this.toDto(model)));\r\n")
                .append("        return dtoList;\r\n")
                .append("    '}'\r\n")
                .append("\r\n")
                .append("    /**\r\n")
                .append("     * 批量DTO To Model\r\n")
                .append("     *\r\n")
                .append("     * @param dtos 源\r\n")
                .append("     * @return 目标\r\n")
                .append("     */\r\n")
                .append("    @Override\r\n")
                .append("    public List<{0}> toModelList(List<{0}DTO> dtos) '{'\r\n")
                .append("        List<{0}> modelList = new ArrayList<>();\r\n")
                .append("        dtos.forEach(dto -> modelList.add(this.toModel(dto)));\r\n")
                .append("        return modelList;\r\n")
                .append("    '}'\r\n")
                .append("'}'\r\n");

        System.out.println("generate Converter end ");

        return MessageFormat.format(sb.toString(), modelName, Generator.firstCharLower(modelName), classComment.get(modelName));
    }
}
